package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品和对应口味
     * @param dishDTO
     */
    //需要在启动类中配置 @EnableTransactionManagement 来开启注解方式的事务管理
    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        log.info("插入菜品和味道");
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);
        //获取insert生成的主键值
        Long dishId = dish.getId();
        //向口味表插入多条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBanch(flavors);
        }
    }
    /**
     * 菜品分页
     * @param dishPageQueryDTO
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuerry(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据Id查找菜品
     */
    @Transactional
    @Override
    public DishVO getbyid(Long id) {
        //获取菜品属性
        Dish getbyid = dishMapper.getbyid(id);
        //获取口味属性
        List<DishFlavor> getfalvorbyid = dishMapper.getfalvorbyid(id);
        //创建DishVO类
        DishVO dishVO = new DishVO();
        //将数据库中根据id查出来的的口味属性赋值给DishVO
        dishVO.setFlavors(getfalvorbyid);
        //将数据库中根据id查出来的属性复制给dishVO
        BeanUtils.copyProperties(getbyid,dishVO);
        return dishVO;
    }
    /**
     * 批量删除菜品
     */
    @Transactional
    @Override
    public void deleteBench(List<Long> ids) {
    //判断当前菜品是否能够删除
        for (Long id : ids) {
            Dish dish = dishMapper.getbyid(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
    //判断菜品是否被套餐关联
        List<Long> setmealDishIds = setmealDishMapper.getSetmealDishIds(ids);
        if (setmealDishIds!=null&&setmealDishIds.size()>0){
            //当前菜品被套餐关联
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
    //删除菜品表中的菜品数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品关联的口味数据
            dishFlavorMapper.deleteByDishId(id);
        }

    }
    /**
     * 修改菜品及菜品口味
     */
    @Override
    public void update(DishDTO dishDTO) {
        //修改菜品
        Dish dish = new Dish();
        //将传进来的菜品参数封装到菜品（dish）里
        BeanUtils.copyProperties(dishDTO,dish);
        log.info("跟新菜品信息");
        dishMapper.update(dish);
        //删除现有口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());
        //修改口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insertBanch(flavors);
        }
    }
    /**
     * 根据类型查询分类
     * @param dish 1 菜品分类 2 套餐分类
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        //根据categoryId查询菜品
        List<Dish> dishList = dishMapper.list(dish);
        //new一个DishVO的List以便封装
        ArrayList<DishVO> dishVOS = new ArrayList<>();
        //遍历dishList，拷贝其中的信息，封装到dishVOS
        for (Dish d: dishList){
            //new一个dishVO，以便接收每次遍历dishList拷贝的信息
            DishVO dishVO = new DishVO();
            //拷贝dishList中的信息到dishVO
            BeanUtils.copyProperties(d,dishVO);
            //根据菜品ID查询口味信息
            List<DishFlavor> flavors = dishFlavorMapper.getBydishId(d.getId());
            //把查到的口味信息封装到dishVO
            dishVO.setFlavors(flavors);
            //把dishVO添加到dishVOS集合，以便返回
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }
    /**
     * 根据分类Id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.getByCategoryId(categoryId);
        return dishList;
    }
    /**
     * 更改菜品售卖状态
     * @param status
     */
    @Override
    public void setStatus(Integer status,Long id) {
        dishMapper.setStatus(status,id);
    }

}
